package org.emavaj.sanguosha.view.gameview;

import static org.emavaj.sanguosha.model.constants.Constants.AUDIO_HIT;
import static org.emavaj.sanguosha.model.constants.Constants.AUDIO_PEACH;
import static org.emavaj.sanguosha.model.constants.Constants.AUDIO_SHAN_FEMALE;
import static org.emavaj.sanguosha.model.constants.Constants.AUDIO_SHAN_MALE;
import static org.emavaj.sanguosha.model.constants.Constants.AUDIO_SHA_FEMALE;
import static org.emavaj.sanguosha.model.constants.Constants.AUDIO_SHA_MALE;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_DAO_GUANG;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_DASHBOARD_AVATAR;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_DASHBOARD_BACK;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_DASHBOARD_EQUIP;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_HP_SMALL;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_PEACH;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_ROLE;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_SHA;
import static org.emavaj.sanguosha.model.constants.Constants.IMG_SHAN;
import static org.emavaj.sanguosha.model.constants.Constants.SELECTED_TAG;
import static org.emavaj.sanguosha.model.constants.Constants.UNSELECTED_TAG;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

import org.emavaj.common.AudioUtil;
import org.emavaj.common.ImageUtil;
import org.emavaj.common.annotation.ActionListenerFor;
import org.emavaj.common.annotation.ActionListenerInstaller;
import org.emavaj.sanguosha.controller.card.HandoutCardController;
import org.emavaj.sanguosha.model.card.AbstractCard;
import org.emavaj.sanguosha.model.card.Skill;
import org.emavaj.sanguosha.model.card.character.Character;
import org.emavaj.sanguosha.model.player.Phase;
import org.emavaj.sanguosha.model.player.Role;
import org.emavaj.sanguosha.view.component.SGSGameButton;
import org.emavaj.sanguosha.view.component.handcard.HandCardLabel;

/**
 * User: DIZEM
 * Time: 11-4-1 下午7:17
 * 
 * eMavaj updated 20120914
 */
public class DashboardPane extends JLayeredPane {

	private static final long serialVersionUID = -6317973981081196515L;
	
	private static final int posY = 30;
	private static final int equipX = 0;
	private static final int cardX = 134;
	private static final int avatarX = cardX + 480;

	private JButton[] btnSkills;
	private JButton btnOK;
	private JButton btnCancel;

	private Character character;
	private Image imgAvatar;
	private Image imgKingdom;
	private String playerName;

	private int labelDisplayLevel = 100;
	private boolean characterChoosed = false;
	private GameFrame owner;
	
	private Map<AbstractCard, HandCardLabel> handCardLabelMap = new HashMap<AbstractCard, HandCardLabel>();
	private List<HandCardLabel> handCardLabelList = new LinkedList<HandCardLabel>();
	
	private HandoutCardController handout;
	
	private int discardCnt = 0;

	public DashboardPane(String playerName, GameFrame owner) {
		super();
		this.owner = owner;
		//this.handout = owner.getHandout();//new HandoutCardController();
		this.playerName = playerName;
		// 布局
		setSize(480 + IMG_DASHBOARD_AVATAR.getWidth(null)
				+ IMG_DASHBOARD_EQUIP.getWidth(null), IMG_DASHBOARD_AVATAR.getHeight(null) + 30);
		setPreferredSize(getSize());
		setOpaque(false);
		// 初始化按钮
		initButtons();
		
		// 注册按钮点击事件
		ActionListenerInstaller.processAnnotation(this);
	}


	public void setCharacter(Character character) {
		this.character = character;
		imgAvatar = ImageUtil.getImage("/generals/big/" + character.getPNGFilename());
		imgKingdom = ImageUtil.getImage("/kingdom/icon/" + character.getKingdomImgName());
		characterChoosed = true;
		initButtons();
		repaint();
	}

	/**
	 * 初始化按钮
	 * 如果角色为选定，则设定确定、取消按钮
	 * 否则根据角色的技能设定技能按钮
	 */
	private void initButtons() {
		if (!characterChoosed) {
			btnOK = new SGSGameButton("确定");
			btnOK.setLocation(avatarX, 0);
			add(btnOK);

			btnCancel = new SGSGameButton("取消");
			btnCancel.setLocation(avatarX + 62, 0);
			btnCancel.setEnabled(true);
			add(btnCancel);

		} else {
			// 技能按钮
			btnSkills = new JButton[character.getSkills().length];
			for (int i = 0; i < btnSkills.length; ++i) {
				Skill skill = character.getSkills()[i];
				// 为每个技能创建按钮
				btnSkills[i] = new SGSGameButton(skill.getName(), skill.getHtmlDescription());
				btnSkills[i].setLocation(avatarX + i * 62 + i, 140 + posY);
				//btnSkills[i].addActionListener(listener);
				add(btnSkills[i]);
			}
			if (btnSkills.length == 1) {
				btnSkills[0].setSize(124, 30);
			}
		}
	}

	/**
	 * 重绘组建
	 * 
	 * @param g
	 */
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(IMG_DASHBOARD_BACK, cardX, 30, null);
		((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g.drawImage(IMG_DASHBOARD_EQUIP, equipX, posY, null);
		g.drawImage(IMG_DASHBOARD_AVATAR, avatarX, 0, null);
		if (imgAvatar != null) {
			g.drawImage(imgAvatar, avatarX + 23, 64, null);
		}
		if (imgKingdom != null) {
			g.drawImage(imgKingdom, avatarX + 21, 62, null);
		}

		// 血条
		if (characterChoosed) {
			int rate = (int) ((double) owner.getCurrentPlayer().getCharacter()
					.getLife()
					/ owner.getCurrentPlayer().getCharacter().getMaxLife() * 5 + 0.01);
			for (int i = 0; i < owner.getCurrentPlayer().getCharacter()
					.getLife(); ++i) {
				g.drawImage(IMG_HP_SMALL[rate], avatarX + 2, 70 + i * 23, null);
			}
			for (int i = owner.getCurrentPlayer().getCharacter().getLife(); i < owner
					.getCurrentPlayer().getCharacter().getMaxLife(); ++i) {
				g.drawImage(IMG_HP_SMALL[0], avatarX + 2, 70 + i * 23, null);
			}
		}
		// 名字
		g.setColor(Color.WHITE);
		int nameWidth = g.getFontMetrics().stringWidth(playerName);
		g.drawString(playerName, avatarX + (122 - nameWidth) / 2 + 8, 53);
	}
	
	private static final List<AbstractCard> EMPTY_CARD_LIST = new ArrayList<AbstractCard>();
	
	public void phaseChangeHook(Phase phase) {

		if (phase.equals(Phase.PLAY)) {
			handout.setMaxSelectCnt(1);
		} else if (phase.equals(Phase.DISCARD)) {
			if (handout.getMaxSelectCnt() == 0) {
				setAllCardUnselectAble();
				owner.getClient().discardCards(EMPTY_CARD_LIST);
				return;
			}
		}
		
		showAvailableCards();
	}
	
	private void showAvailableCards() {
		for (HandCardLabel label : handCardLabelList) {
			label.setSelectAble(label.getCard().availableInTheState(owner));
		}
	}
	
	private void setAllCardUnselectAble() {
		for (HandCardLabel label : handCardLabelList) {
			unselectOneCard(label);
			label.setSelectAble(false);
		}
	}
	
//	/**
//	 * 反馈阶段，取得对方出的牌
//	 * @return
//	 */
//	public AbstractCard getBeOfferedCard() {
//		return owner.getBeOfferedCardInfo().getSendCard();
//	}
	
	/**
	 * 取得当前阶段
	 * @return
	 */
	public Phase getCurrentPhase() {
		return owner.getCurrentPlayer().getPhase();
	}
	
	/**
	 * 选择一张牌
	 * 
	 * @param label
	 */
	public void selectCardEffect(HandCardLabel label) {

		selectOneCard(label);

		AbstractCard card = label.getCard();
		// selectedCardList.add(label.getCard());
		handout.addOneCard(card);

		Phase phase = owner.getCurrentPlayer().getPhase();
		if (phase.equals(Phase.PLAY)) {
			// 点亮可选的目标玩家
			card.canOfferTo(owner);
		}

		// 是否已经准备就绪
		if (!handout.canSelectMore()) {
			unselectAbelExcept(handout.getPlayCardList());
		}
		if (canPlayTheSelectedCard(phase)) {
			btnOK.setEnabled(true);
		}
	}
	
	/**
	 * 只用于出牌阶段
	 * 
	 * @return
	 */
	public boolean canPlayTheSelectedCard(Phase phase) {
//		if (phase.equals(Phase.PLAY)) {
//			return handout.getPlayCard().alreadyCanOffer(owner);
//		} else if (phase.equals(Phase.DISCARD)) {
//			return handout.selectCardCnt() == handout.getMaxSelectCnt();
//		}
		switch (phase) {
		case PLAY:
		case FEEDBACK:
			return handout.getPlayCard().alreadyCanOffer(owner);
		case DISCARD:
			return handout.selectCardCnt() == handout.getMaxSelectCnt();
		default:
			return false;
		}
	}
	
	private void unselectAbelExcept(List<AbstractCard> playCardList) {
		for (HandCardLabel label : handCardLabelList) {
			AbstractCard card = label.getCard();
			if (!playCardList.contains(card)) {
				handout.removeOneCard(card);
				unselectOneCard(label);
				label.setSelectAble(false);
			}
		}
	}
	
	private List<AbstractCard> emptyList = new ArrayList<AbstractCard>();

	/**
	 * 取消牌的选择状态
	 * @param label
	 */
	public void cancelCardEffect(HandCardLabel label) {
		
		unselectOneCard(label);
		
		handout.removeOneCard(label.getCard());

		// 取消所有选中了的角色
		Phase phase = getCurrentPhase();
		if (phase.equals(Phase.PLAY)) {
			// 点亮可选的目标玩家
			owner.setAllPaneCannotSelect();
		}
		
		unselectAbelExcept(emptyList);
		
		showAvailableCards();
		
		if (handout.isSelectedNoCard()) {
			btnOK.setEnabled(false);
		}
	}

	/**
	 * 用手牌创建label
	 *
	 * @param card 手牌
	 * @return label
	 */
	private HandCardLabel createCardLabel(AbstractCard card) {

		HandCardLabel label = new HandCardLabel(card, owner, this);

		handCardLabelMap.put(card, label);

		/*
		// 注册单击事件
		UIUtil.actionLabel(label, new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				// 如果玩家在NOT_ACTIVE阶段 不作处理
				if (owner.getCurrentPlayer().getPhase()
						.equals(Phase.NOT_ACTIVE))
					return;

				// 如果当前手牌已经选择，则撤销选中
				if (label.getName().equals(SELECTED_TAG)) {
					
					cancelCardEffect(label);
					
				} else if (canUseCard(handCardLabelMap.getKey(label))) {
					label.setName(SELECTED_TAG);
					if (canSelectCardCount == selectedCardList.size()) {
						unselectFirstSelectedCard();
					}
					selectBasicCard(handCardLabelMap.getKey(label));

					selectCardEffect(label);

					if (owner.getCurrentPlayer().getPhase() == Phase.DISCARD) {
						System.out.println(canSelectCardCount);
						btnOK.setEnabled(selectedCardList.size() == canSelectCardCount);
					}
				}
			}
		});
		*/
		return label;
	}
	
//	public AbstractCard getCardOfLabel(HandCardLabel label) {
//		return handCardLabelMap.getKey(label);
//	}

//	/**
//	 * 用装备牌创建label
//	 *
//	 * @param card 装备牌
//	 * @return label
//	 */
//	private HandCardLabel createEquipmentLabel(final AbstractEquipmentCard card) {
////		final HandCardLabel label = new EquipmentLabel(card);
////		equipmentLabelMap.put(card.getEquipmentType(), label);
////		label.setLocation(10, 42 + posY + card.getEquipmentType() * 32);
////		return label;
//		return null;
//	}

	/**
	 * 调整手牌放置间距
	 */
	private void updateHandCardGap() {
		int gap;
		//如果手牌总宽度小于面板 则不需要重叠
		if (480 > handCardLabelList.size() * 90) {
			gap = 90;

		} else { //计算重叠间距
			gap = 390 / (handCardLabelList.size() - 1);
		}
		for (int i = 0; i < handCardLabelList.size(); ++i) {
			handCardLabelList.get(i).setLocation(cardX + i * gap, posY + 38);
		}
	}

	public void addHandCard(AbstractCard card) {
		addHandCardLabel(createCardLabel(card));
		repaint();
	}

	public void addHandCardLabel(HandCardLabel handCardLabel) {
		add(handCardLabel, new Integer(labelDisplayLevel++));
		handCardLabelList.add(handCardLabel);
		updateHandCardGap();
	}

	private void removeHandCardLabel(AbstractCard removeCard) {
		HandCardLabel label = handCardLabelMap.get(removeCard);
		remove(label);
		label.repaint();
		handCardLabelList.remove(label);
		handCardLabelMap.remove(removeCard);
//		unselectedUseCard();
		updateHandCardGap();
		handout.clear();
	}

//	private void addEquipmentCardLabel(HandCardLabel equipmentCardLabel) {
//		add(equipmentCardLabel, new Integer(labelDisplayLevel++));
//	}

//	private void removeEquipmentCardLabel(HandCardLabel equipmentCardLabel) {
//		remove(equipmentCardLabel);
//		equipmentLabelMap.removeByValue(equipmentCardLabel);
//	}

	@ActionListenerFor(source = "btnOK")
	public void handleOKButton() {
		Phase phase = getCurrentPhase();
		switch (phase) {
		case PLAY:
			useHandCard();
			break;
		case FEEDBACK:
			feedback(handout.getPlayCard());
			
			break;
		case DISCARD:
			discardHandCard();
//			canSelectCardCount = 1;
			break;
		case FINISH:
			//break;
		case NOT_ACTIVE:
			//break;
		case WAIT_OTHER:
			//break;
		default:
			// default case do nothing!
			break;
		}
		btnOK.setEnabled(false);
	}
	
	@ActionListenerFor(source = "btnCancel")
	public void handleCancelButton() {
		switch (owner.getCurrentPlayer().getPhase()) {
		case PLAY:
			owner.getClient().sendEndPlayInfo();
			break;
		case FEEDBACK:
			feedback(null);
			break;
		case START:
			break;
		case JUDGE:
			break;
		case DRAW:
			break;
		case DISCARD:
			break;
		case FINISH:
			break;
		case NOT_ACTIVE:
			break;
		case WAIT_OTHER:
			break;
		default:
			// default case do nothing!
			break;
		}
	}

	private void feedback(AbstractCard cardLabel) {
		
		if (cardLabel != null) {
			removeHandCardLabel(cardLabel);
			//feedback(selectedCard.getCard());
			owner.getClient().sendFeedbackInfo(cardLabel);
		} else {
			owner.getClient().sendFeedbackInfo(null);
			// TODO: card work
		}
		
		//if (owner.getFeedbackCard().getName().equals("杀")) {

//			if (cardLabel == null) {
//				owner.getClient().sendDecreaseLifeInfo();
//			}
		
		

			//owner.getCurrentPlayer().setPhase(Phase.NOT_ACTIVE);
		owner.changeCurrentPlayerPhase(Phase.NOT_ACTIVE);
			
			
//			unselectedUseCard();
			
			repaint();
		//}
	}


	/**
	 * 设置当前玩家的角色势力
	 *
	 * @param role 角色势力
	 */
	public void setRole(final Role role) {
		owner.getCurrentPlayer().setRole(role);
		final JLabel roleLabel = new JLabel(IMG_ROLE[owner.getCurrentPlayer().getRoleID()]);
		roleLabel.setSize(IMG_ROLE[owner.getCurrentPlayer().getRoleID()].getIconWidth(),
				IMG_ROLE[owner.getCurrentPlayer().getRoleID()].getIconHeight());
		final int x = avatarX - 3, y = 140;
		roleLabel.setLocation(x, y);

		//将势力label天添加至面板最上层
		add(roleLabel, new Integer(labelDisplayLevel++));

		//将势力label平移至相应坐标
		new Thread(new Runnable() {
			public void run() {
				while (roleLabel.getY() > 38) {
					roleLabel.setLocation(x, roleLabel.getY() - 2);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	/**
	 * 显示出牌动画效果
	 *
	 * @param card 出牌
	 */
	public void showEffect(AbstractCard card) {
		final JLabel effectLabel = new JLabel();
		effectLabel.setSize(256, 256);
		effectLabel.setOpaque(false);
		effectLabel.setLocation(400, -50);
		add(effectLabel, new Integer(labelDisplayLevel++));

		ImageIcon[] imgList = new ImageIcon[0];
		if (card.getName().equals("杀")) {
			AudioUtil.play(owner.getCurrentPlayer().getCharacter().isMale() ? AUDIO_SHA_MALE : AUDIO_SHA_FEMALE);
			imgList = IMG_SHA;


		} else if (card.getName().equals("闪")) {
			AudioUtil.play(owner.getCurrentPlayer().getCharacter().isMale() ? AUDIO_SHAN_MALE : AUDIO_SHAN_FEMALE);
			imgList = IMG_SHAN;

		} else if (card.getName().equals("桃")) {
			AudioUtil.play(AUDIO_PEACH);
			imgList = IMG_PEACH;
		} else {
			return;
		}


		final ImageIcon[] finalImgList = imgList;
		new Thread(new Runnable() {
			public void run() {
				for (ImageIcon img : finalImgList) {
					effectLabel.setIcon(img);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

				remove(effectLabel);
				repaint();
			}
		}).start();
	}

//	public void addEquipmentCard(AbstractEquipmentCard equipmentCard) {
//
//		if (!owner.getCurrentPlayer().canAddEquipmentCard(equipmentCard)) {
//			HandCardLabel cardLabelToRemove = equipmentLabelMap.getValue((equipmentCard).getEquipmentType());
//			removeEquipmentCardLabel(cardLabelToRemove);
//			owner.getCurrentPlayer().removeEquipmentCard(equipmentCard.getEquipmentType());
//		}
//		owner.getCurrentPlayer().addEquipmentCard(equipmentCard);
//		addEquipmentCardLabel(createEquipmentLabel(equipmentCard));
//		repaint();
//	}

	/**
	 * 使用选中的手牌
	 */
	public void useHandCard() {
		// Iterator<HandCardLabel> itCardLabel = selectedCardList.iterator();

		if (handout.isSelectedNoCard()) {
			return;
		}
		// while (itCardLabel.hasNext()) {
		// HandCardLabel label = itCardLabel.next();
		AbstractCard card = handout.getPlayCard();

		// 出牌的抽象调用，将职责交给牌
		card.runCardEffect(owner);
		// selectedCard.beUsed();
		removeHandCardLabel(card);
		// itCardLabel.remove();
		// }

		if (card.needsFeedBack()) {
//			owner.getCurrentPlayer().setPhase(Phase.WAIT_OTHER);
			owner.changeCurrentPlayerPhase(Phase.WAIT_OTHER);
			//phaseChangeHook(Phase.WAIT_OTHER);
		}

		repaint();
	}

	/**
	 * 弃手牌
	 */
	private void discardHandCard() {
		Iterator<AbstractCard> itCards = handout.getPlayCardList().iterator();
		//List<AbstractCard> playerHandCards = owner.getCurrentPlayer().getHandCards();
		List<AbstractCard> cardList = new ArrayList<AbstractCard>();
		while (itCards.hasNext()) {
			AbstractCard card = itCards.next();
			cardList.add(card);
			removeHandCardLabel(card);
			//playerHandCards.remove(card);
			itCards.remove();
		}

		owner.getClient().discardCards(cardList);
		handout.clear();
		owner.setAllPaneCannotSelect();
		//showAvailableCards();
		setAllCardUnselectAble();
		repaint();
	}

//	private boolean hasOffedSha = false;
//
//	/**
//	 * 使用基本牌
//	 * @param card
//	 */
//	public void useBasicCard(AbstractCard card) {
//		if (card.getName().equals("杀")) {
//			hasOffedSha = true;
//			showEffect(card);
//			for (OtherPlayerPane pane : owner.getOtherPlayerPaneList()) {
//				if (pane.isSelected()) {
//					pane.setSelected(false);
//					owner.offerCardTo(card, pane.getPlayer().getPlayerId());
//					return;
//				}
//			}
//		} else if (card.getName().equals("桃")) {
//			owner.getClient().useHandCard(card);
//		}
//
////		for (OtherPlayerPane pane : owner.otherPlayerPaneList) {
////			if (pane.isSelected())
////				pane.setSelected(false);
////		}
//	}

	public void selectBasicCard(AbstractCard card) {

		if (card.getName().equals("杀") || card.getName().equals("决斗")) {
			for (OtherPlayerPane pane : owner.getOtherPlayerPaneList()) {
				if (Math.abs(pane.getPlayer().getPlayerId() - owner.getCurrentPlayerID()) <= 1) { //todo fixme
					pane.setCanSelect(true);
				} else {
					pane.setCanSelect(false);
				}
			}
		}
	}

	/**
	 * 是否可以出牌
	 *
	 * @param flag
	 */
	public void setOfferable(boolean flag) {
		btnOK.setEnabled(flag);
	}

//	public void setHasOffedSha(boolean hasOffedSha) {
//		this.hasOffedSha = hasOffedSha;
//	}

	/**
	 * 是否可以弃权
	 * @param flag
	 */
	public void setCancelable(boolean flag) {
		btnCancel.setEnabled(flag);
	}

	/**
	 * 减血动画
	 */
	public void decreaseLife() {
		owner.getCurrentPlayer().getCharacter().decreaseLife();
		repaint();
		final JLabel labelLight = new JLabel();
		labelLight.setSize(172, 170);
		labelLight.setOpaque(false);
		labelLight.setLocation(avatarX - 20, 20);
		add(labelLight, new Integer(labelDisplayLevel++));

		new Thread(new Runnable() {
			public void run() {
				for (ImageIcon img : IMG_DAO_GUANG) {
					labelLight.setIcon(img);
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				remove(labelLight);
				repaint();
			}
		}).start();
		AudioUtil.play(AUDIO_HIT);
		repaint();
	}
	
	public void discard(int n) {
		handout.setMaxSelectCnt(n);
		discardCnt = handout.getMaxSelectCnt();
	}
	
	public int getDiscardCnt() {
		return discardCnt;
	}

	public List<AbstractCard> getSeletedCards() {
		//return handCardLabelMap.get(handout.getFirstCard());
		return handout.getPlayCardList();
	}
	
	private void selectOneCard(HandCardLabel label) {
		label.setName(SELECTED_TAG);
		label.setLocation(label.getX(), posY + 38 - 30);
		DashboardPane.this.repaint(label.getX(), label.getY(), 90,
				160);
	}
	
	private void unselectOneCard(HandCardLabel label) {
		label.setName(UNSELECTED_TAG);
		label.setLocation(label.getX(), 38 + 30);
		DashboardPane.this.repaint(label.getX(), label.getY() - 30,
				90, 160);
	}
	
	/**
	 * 检查出牌目标是否足够
	 */
	public void checkTagetEnough() {
		if (handout.isSelectedNoCard()) {
			owner.setAllPaneCannotSelect();
		}
		//int max = dashboard.getUseCard().getCard().canSelectPlayerCount();
		AbstractCard card = handout.getPlayCard();
		int max = card.canSelectPlayerCount();
		for (OtherPlayerPane pane : owner.getOtherPlayerPaneList()) {
			if (pane.isSelected()) {
				--max;
			}
		}
		if (max <= 0) {	// 已经凑够出牌目标，让其他所有目标不可选择
			for (OtherPlayerPane pane : owner.getOtherPlayerPaneList()) {
				if (!pane.isSelected()) {
					pane.setCanSelect(false);
				}
			}
			if (canPlayTheSelectedCard(getCurrentPhase())) {
				btnOK.setEnabled(true);
			}
		} else {	// 未够出牌目标，让候选目标可选择
			//dashboard.getUseCard().getCard().canOfferTo(this);
			card.canOfferTo(owner);
		}
	}

	public void setHandout(HandoutCardController handout) {
		this.handout = handout;
	}
}
